﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.UI;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace Mazik.WinRT.Components.Controls
{
    [TemplatePart(Name = MzkSignature.ShowPopupButton, Type = typeof(Button))]
    [TemplatePart(Name = MzkSignature.TitleTextBlock, Type = typeof(TextBlock))]
    [TemplatePart(Name = MzkSignature.SignatureImage, Type = typeof(Image))]
    public sealed class MzkSignature : Control
    {
        #region Signature Property
        public SignaturePage SignPopup { get; set; }
        #endregion

        public MzkSignature()
        {
            this.DefaultStyleKey = typeof(MzkSignature);
        }

        private const string ShowPopupButton = "myButton";
        private const string TitleTextBlock = "title";
        private const string SignatureImage = "imgOriginator";

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var btn = this.GetTemplateChild(MzkSignature.ShowPopupButton) as Button;
            var title = this.GetTemplateChild(MzkSignature.TitleTextBlock) as TextBlock;
            btn.Click += btn_Click;
            title.Text = this.Title;

        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            CoreWindow currentWindow = Window.Current.CoreWindow;
            var img = this.GetTemplateChild(MzkSignature.SignatureImage) as Image;
            Popup popup = new Popup();
            popup.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;
            popup.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Stretch;

            if (SignPopup == null)
            {
                SignPopup = new SignaturePage();

            }
            else
            {
                SignPopup = null;
                this.SignPopup = new SignaturePage();
            }
            popup.Closed += popup_Closed;
            var vistaColorBrush = new SolidColorBrush(this.AccentColor);
            vistaColorBrush.Opacity = 0.7;
            this.SignPopup.VistaColor = vistaColorBrush;
            this.SignPopup.AccentColor = new SolidColorBrush(this.AccentColor);
            popup.Child = SignPopup;
            this.SignPopup.Tag = popup;
            img.Tag = this.FileName;
            this.SignPopup.Source = img;
            ImageSource = (BitmapImage)this.SignPopup.Source.Source;
            this.SignPopup.Height = currentWindow.Bounds.Height;
            this.SignPopup.Width = currentWindow.Bounds.Width;
            var btn = (Button)sender;
            popup.Margin = new Thickness(0, 0, 0, 0);
            popup.IsOpen = true;
        }

        void popup_Closed(object sender, object e)
        {
            var popup = (Popup)sender;
            SignaturePage page = (SignaturePage)popup.Child;
            this.ImageSource =(BitmapImage) page.Source.Source;
        }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(MzkSignature), new PropertyMetadata(0));



        public Color AccentColor
        {
            get
            {
                var color = GetValue(AccentColorProperty);

                return color is int ? Color.FromArgb(255, 25, 25, 112) : (Color)color;

            }
            set { SetValue(AccentColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AccentColor.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty AccentColorProperty =
            DependencyProperty.Register("AccentColor", typeof(Color), typeof(MzkSignature), new PropertyMetadata(0));


        public string FileName
        {
            get
            {
                return (string)GetValue(FileNameProperty);
            }
            set
            {
                SetValue(FileNameProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for FileName.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty FileNameProperty =
            DependencyProperty.Register("FileName", typeof(string), typeof(MzkSignature), new PropertyMetadata(Path.GetRandomFileName().Replace('.', 'z')));




        


        public BitmapImage ImageSource
        {
            get { return (BitmapImage)GetValue(ImageSourceProperty); }
            set { SetValue(ImageSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...
        private static readonly DependencyProperty ImageSourceProperty =
            DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(MzkSignature), new PropertyMetadata(0));


        public string GetFileName()
        {
            return this.FileName;
        }


    }
}
